package _2025_4_16;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class FindByname1 {
    public static void main(String[] args) throws IOException {
        //
        System.out.println("请输入要扫描的路径");
        Scanner scanner=new Scanner(System.in);
        String rootpath=scanner.next();
        //传入文件
        File file=new File(rootpath);
        String key= scanner.next();
        if(!file.exists()){
            System.out.println("路径不存在");
            return;
        }
       if(!file.isDirectory()){
            System.out.println("输入的路径不是目录");
            return;
        }
        if(rootpath.isEmpty()||rootpath.length()==0){
            System.out.println("输入的字符串不能为空");
            return;
        }
        //扫描文件所含内容
        scan(file,key);
    }
    private static void scan(File file,String key) throws IOException {
        File[] files=file.listFiles();
        if(files.length==0)return;
        //循环
        for (File file1:files){
            //如果是文件进入判断
            if(file1.isFile()){
                String name=file1.getName();
                if(name.contains(key)){
                    System.out.println("找到文件："+file1.getCanonicalPath()+"是否删除?(Y/N)");
                    Scanner scanner=new Scanner(System.in);
                    String ret=scanner.next();
                    if(ret.equals("Y")){
                        file1.delete();
                        System.out.println("删除文件："+file1.getCanonicalPath());
                    }
                }
            }
            else{
                scan(file1,key);
            }
        }
    }
}
